/*
 * Copyright (c) 2023 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.executor;


import cn.xphsc.kubernetes.core.query.NamespaceQuery;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import org.apache.commons.lang3.StringUtils;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description:  用于获取Kubernetes中的所有命名空间的list Executor
 * @since 1.0.0
 */
public class ListNamespaceExecutor<T> extends AbstractExecutor<T> {
    private String listNamespaceName;
    private  T entity;
    private  CoreV1Api  coreV1Api;
    private NamespaceQuery namespaceQuery;
    private AppsV1Api appsv1api;
    public ListNamespaceExecutor(ApiClient client, NamespaceQuery namespaceQuery, ListNamespaceMode listNamespaceMode) {
        super(client);
        this.listNamespaceName=listNamespaceMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.appsv1api=new AppsV1Api(this.getClient());
        this.namespaceQuery=namespaceQuery;
    }

    @Override
    protected  T doExecute() {
        if (StringUtils.isNotBlank(listNamespaceName)) {
            switch (listNamespaceName){
                case "NS":
                    try {
                        entity = (T) coreV1Api.listNamespace(namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICE":
                    try {
                        entity = (T) coreV1Api.listNamespacedService(namespaceQuery.namespace(), namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;

                case "DEPLY":
                    try {
                        entity = (T) appsv1api.listNamespacedDeployment(namespaceQuery.namespace(),namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLNSDEPLY":
                    try {
                        entity = (T) appsv1api.listDeploymentForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;

                case "ENDPOINTS":
                    try {
                        entity = (T) coreV1Api.listNamespacedEndpoints(namespaceQuery.namespace(), namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PV":
                    try {
                        entity = (T) coreV1Api.listPersistentVolume(namespaceQuery.pretty(),namespaceQuery.allowWatchBookmarks(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PVC":
                    try {
                        entity = (T) coreV1Api.listNamespacedPersistentVolumeClaim(namespaceQuery.namespace(), namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLNSPVC":
                    try {
                        entity = (T) coreV1Api.listPersistentVolumeClaimForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "CM":
                    try {
                        entity = (T) coreV1Api.listNamespacedConfigMap(namespaceQuery.namespace(), namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLNSCM":
                    try {
                        entity = (T) coreV1Api.listConfigMapForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "POD":
                    try {
                        entity = (T) coreV1Api.listNamespacedPod(namespaceQuery.namespace(), namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLNSPOD":
                    try {
                        entity = (T) coreV1Api.listPodForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "DS":
                    try {
                        entity = (T) appsv1api.listNamespacedDaemonSet(namespaceQuery.namespace(), namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLDS":
                    try {
                        entity = (T) appsv1api.listDaemonSetForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PODTEMPLATE":
                    try {
                        entity = (T) coreV1Api.listNamespacedPodTemplate(namespaceQuery.namespace(), namespaceQuery.pretty(),namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLSA":
                    try {
                        entity = (T) coreV1Api.listServiceAccountForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SA":
                    try {
                        entity = (T) coreV1Api.listNamespacedServiceAccount(namespaceQuery.namespace(), namespaceQuery.pretty(),namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "NODE":
                    try {
                        entity = (T) coreV1Api.listNode(namespaceQuery.pretty(),namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "REPLICASET":
                    try {
                        entity = (T) appsv1api.listNamespacedReplicaSet(namespaceQuery.namespace(), namespaceQuery.pretty(),namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLREPLICASET":
                    try {
                        entity = (T) appsv1api.listReplicaSetForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "STATEFULSET":
                    try {
                        entity = (T) appsv1api.listNamespacedStatefulSet(namespaceQuery.namespace(), namespaceQuery.pretty(),namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLSTATEFULSET":
                    try {
                        entity = (T) appsv1api.listStatefulSetForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "EVENT":
                    try {
                        entity = (T) coreV1Api.listNamespacedEvent(namespaceQuery.namespace(), namespaceQuery.pretty(),namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ALLEVENT":
                    try {
                        entity = (T) coreV1Api.listEventForAllNamespaces(namespaceQuery.allowWatchBookmarks(),namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                default:

            }
        }
        return (T) entity;
    }
    public static enum ListNamespaceMode {
        /**
         *
         */
        NS,
        SERVICE,
        DEPLY,
        ALLNSDEPLY,
        ENDPOINTS,
        PV,
        ALLNSPV,
        PVC,
        ALLNSPVC,
        CM,
        ALLNSCM,
        ASYNC,
        POD,
        ALLNSPOD,
        DS,
        ALLDS,
        PODTEMPLATE,
        SA,
        ALLSA,
        REPLICASET,
        ALLREPLICASET,
        STATEFULSET,
        ALLSTATEFULSET,
        EVENT,
        ALLEVENT,
        NODE,
        AUTO;
        private ListNamespaceMode() {

        }
        private ListNamespaceMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


